package com.example.demo.controller;

import com.example.demo.service.DmnDecisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * DMN 决策控制器
 * 演示 Flowable DMN 引擎的使用
 */
@RestController
@RequestMapping("/api/dmn")
public class DmnController {

    @Autowired
    private DmnDecisionService dmnDecisionService;

    /**
     * 请假审批决策示例
     */
    @PostMapping("/leave-approval")
    public Map<String, Object> leaveApprovalDecision(@RequestBody Map<String, Object> request) {
        System.out.println("=== 请假审批 DMN 决策示例 ===");
        
        String employee = (String) request.get("employee");
        String employeeLevel = (String) request.get("employeeLevel");
        Integer days = (Integer) request.get("days");
        String leaveType = (String) request.get("leaveType");
        
        System.out.println("输入参数：");
        System.out.println("- 员工：" + employee);
        System.out.println("- 员工级别：" + employeeLevel);
        System.out.println("- 请假天数：" + days);
        System.out.println("- 请假类型：" + leaveType);
        
        Map<String, Object> result = dmnDecisionService.executeLeaveApprovalDecision(
                employeeLevel, days, leaveType);
        
        System.out.println("决策结果：" + result);
        System.out.println("================================");
        
        return result;
    }

    /**
     * 风险评估决策示例
     */
    @PostMapping("/risk-assessment")
    public Map<String, Object> riskAssessmentDecision(@RequestBody Map<String, Object> request) {
        System.out.println("=== 风险评估 DMN 决策示例 ===");
        
        String projectType = (String) request.get("projectType");
        Integer budget = (Integer) request.get("budget");
        Integer teamSize = (Integer) request.get("teamSize");
        String experience = (String) request.get("experience");
        
        System.out.println("输入参数：");
        System.out.println("- 项目类型：" + projectType);
        System.out.println("- 预算：" + budget);
        System.out.println("- 团队规模：" + teamSize);
        System.out.println("- 经验水平：" + experience);
        
        Map<String, Object> result = dmnDecisionService.executeRiskAssessmentDecision(
                projectType, budget, teamSize, experience);
        
        System.out.println("决策结果：" + result);
        System.out.println("================================");
        
        return result;
    }

    /**
     * 折扣计算决策示例
     */
    @PostMapping("/discount-calculation")
    public Map<String, Object> discountCalculationDecision(@RequestBody Map<String, Object> request) {
        System.out.println("=== 折扣计算 DMN 决策示例 ===");
        
        String customerType = (String) request.get("customerType");
        Double orderAmount = (Double) request.get("orderAmount");
        Integer loyaltyPoints = (Integer) request.get("loyaltyPoints");
        String season = (String) request.get("season");
        
        System.out.println("输入参数：");
        System.out.println("- 客户类型：" + customerType);
        System.out.println("- 订单金额：" + orderAmount);
        System.out.println("- 积分：" + loyaltyPoints);
        System.out.println("- 季节：" + season);
        
        Map<String, Object> result = dmnDecisionService.executeDiscountCalculationDecision(
                customerType, orderAmount, loyaltyPoints, season);
        
        System.out.println("决策结果：" + result);
        System.out.println("================================");
        
        return result;
    }

    /**
     * 获取所有可用的决策表
     */
    @GetMapping("/decisions")
    public Map<String, Object> getAvailableDecisions() {
        return dmnDecisionService.getAvailableDecisions();
    }

    /**
     * 测试所有 DMN 决策示例
     */
    @GetMapping("/test-all")
    public Map<String, Object> testAllDecisions() {
        System.out.println("=== 测试所有 DMN 决策示例 ===");
        
        Map<String, Object> results = new HashMap<>();
        
        // 测试请假审批决策
        Map<String, Object> leaveResult = dmnDecisionService.executeLeaveApprovalDecision(
                "普通员工", 2, "病假");
        results.put("leaveApproval", leaveResult);
        
        // 测试风险评估决策
        Map<String, Object> riskResult = dmnDecisionService.executeRiskAssessmentDecision(
                "软件开发", 100000, 5, "高级");
        results.put("riskAssessment", riskResult);
        
        // 测试折扣计算决策
        Map<String, Object> discountResult = dmnDecisionService.executeDiscountCalculationDecision(
                "VIP", 5000.0, 1000, "春季");
        results.put("discountCalculation", discountResult);
        
        System.out.println("所有决策测试完成");
        System.out.println("================================");
        
        return results;
    }
}

